package falstad;

import java.awt.Container;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class BatteryBarWorker extends SwingWorker<Integer, Integer> {

	
	boolean done = false;
	int batteryLevel;
	Container parent;
	
	JProgressBar batteryBar;
	
	PropertyChangeListener ba = new PropertyChangeListener() {
	     public  void propertyChange(PropertyChangeEvent evt) {
	         if ("battery".equals(evt.getPropertyName())) {
	             batteryBar.setValue((Integer)evt.getNewValue());
	         }
	     }
	};
	
	public BatteryBarWorker(Container c, JProgressBar bar, float robotBatteryLevel)
	{
		this.batteryLevel = (int) robotBatteryLevel;
		this.batteryBar = bar;
		this.parent = c;
		
	}
	
	@Override
	protected Integer doInBackground() throws Exception {
		while (!done && batteryLevel != 0)
		{
			setProgress(batteryLevel);
			batteryBar.setValue(batteryLevel);
			parent.repaint();
			parent.validate();
//			publish(maxBatteryLevel);
		}
		
		return batteryLevel;
	}
	
	
	@Override
	protected void process(List<Integer> currentBatteryLevel)
	{
		batteryBar.setValue(batteryLevel);
	}
	
	
//	@Override
//	public void propertyChange(PropertyChangeEvent evt)
//	{
//		int battery = task.getProgress();
//		progressBar.setValue(battery);
//		taskOutput.append(String.format("Completed %d%% of battery.\n", battery));
//	}
	
	
	
	
	public void done() {
	    //Tell progress listener to stop updating progress bar.
	    done = true;
	   // startButton.setEnabled(true);
	   // setCursor(null); //turn off the wait cursor
	    batteryBar.setValue(batteryBar.getMinimum());
	}
	
	

}
